// json map layer
var JsonMap = cc.Node.extend({
    _childArray : [],
    _awardArray : [],
    _directArray : [],
    ctor:function () {
        this._super();
        return true;
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        this._awardArray.splice(0,this._awardArray.length);
        this._directArray.splice(0,this._directArray.length);
        this._childArray.splice(0,this._childArray.length);
        this._super();
    },
    initWithJsonFile : function(filename){
        if ( filename ){
            var jsonObject = CreateJsonWithFile(filename);
            if( jsonObject ){
                cc.log("initialize json map from file: "+filename);
                return this.layoutMapElements(jsonObject);
            }else{
                cc.log("load json fail,please check your json file: "+filename);
                return false;
            }
        }else{
            cc.log("error: "+filename+" json file name should not be null");
            return false;
        }
    },
    initWithJsonObject : function(jsonobject){
        if( jsonobject ){
            cc.log("initialize json map from json object: "+jsonobject);
            return this.layoutMapElements(jsonobject);
        }else {
            cc.log("invalid json map object: "+jsonobject);
            return false;
        }
    },
    // layout map elements
    layoutMapElements : function(jsonobject){
        cc.log("layout map elements:");
        var jsonObject=jsonobject;
        if(jsonObject){
            var tileWidth = jsonObject.width;
            var tileHeight = jsonObject.height;
            for ( var x=0; x<jsonObject.xGrid ;x++ ){
                var yArray = [];
                for ( var y=0;y<jsonObject.yGrid;y++ ){
                    var objArray = [],haveChild = false;
                    for ( var i=0;i<jsonObject.children.length;i++) {
                        var child = jsonObject.children[i];
                        if( child.x==x && child.y==y ){
                            // have child in (x,y)
                            haveChild = true;
                            // award
                            var childObj = null;
                            if(child.hp){
                                var childObj = new JsonAward("res/"+child.res,child.hp,child.gold);
                                childObj.setPosition( cc.p(tileWidth*(x+0.5),tileHeight*(y+0.5)) );
                                this.addChild(childObj,-child.y+1);
                                this._awardArray.push(childObj);
                            }
                            // road
                            else{
                                childObj = new JsonGrid("res/"+child.res,child.d?child.d:0 );
                                childObj.setPosition( cc.p( tileWidth*(x+0.5),tileHeight*(y+0.5) ) );
                                this.addChild(childObj);
                                // direct
                                if( child.d ){
                                    this._directArray.push(childObj);
                                    cc.log("direct at ("+x+","+y+")");
                                }
                            }
                            objArray.push(childObj);
                            objArray.push(x);
                            objArray.push(y);
                            cc.log("have child ("+x+","+y+") object array length: "+objArray.length);
                            break;
                        }
                    }
                    // not have child in (x,y)
                    if (!haveChild){
                        objArray.push(null);
                        objArray.push(x);
                        objArray.push(y);
                        cc.log("not child ("+x+","+y+") object array length: "+objArray.length);
                    }
                    yArray.push( objArray );
                }
                this._childArray.push(yArray);
            }
            // emitter
            var emit=jsonObject.emitter[0];
            var emitter = new cc.Sprite("res/"+emit.res);
            emitter.setPosition( cc.p(emit.x,emit.y ) );
            this.addChild(emitter);
        }else{
            cc.log("json object invalid");
        }
        return true;
    },
    getChildArray : function(){
        //cc.log("get child array");
        return this._childArray;
    },
    getAwardArray : function(){
        //cc.log("get award array");
        return this._awardArray;
    },
    getDirectArray : function(){
        //cc.log("get direct array");
        return this._directArray;
    },
    // checking point is empty
    isEmptyPoint : function(point){
        for( var y=0;y<this._childArray.length;y++){
            var yArr = this._childArray[y];
            for( var x=0;x<yArr.length;x++){
                var obj = yArr[x];
                if(obj[1]==point.x&&obj[2]==point.y){
                    if(null==obj[0]){
                        return obj;
                    }else{
                        return null;
                    }
                }
            }
        }
        return null;
    }
});
JsonMap.createWithJsonFile=function(filename){
    cc.log("create json map from file: "+filename);
    var jsonMap = new JsonMap();
    jsonMap.initWithJsonFile(filename);
    return jsonMap;
}
JsonMap.createWithJsonObject=function(jsonobject){
    cc.log("create json map from json object: "+jsonobject);
    var jsonMap = new JsonMap();
    jsonMap.initWithJsonObject(jsonobject);
    return jsonMap;
}
